package com.flyoil.petromp.ui.activity.activity_course;

import android.app.Fragment;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.cnpc.fyviewlibrary.viewpager.FragmentPagerAdapter;
import com.flyoil.petromp.R;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.ui.fragment.fragment_order.CourseCertainListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体某一类流程的列表界面
 */
public class CourseCertainListActivity extends BaseActivity {
    private List<Fragment> fragments;
    private ViewPager viewPager;
    private FragmentPagerAdapter adapter;
    //已审批、待审批、草稿箱、撤销
    private TextView tv_approveed, tv_unapprove, tv_drafts, tv_revocation;
    private View line_approveed, line_unapprove, line_drafts, line_revocation;
    private String order_type;
    private int order_status = 0;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_course_certain_list;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTextTitleName("计划列表");
        if (getIntent().getStringExtra("title_name") != null) {
            setTextTitleName(getIntent().getStringExtra("title_name"));
        }
        if (getIntent().getStringExtra("order_type") != null) {
            order_type = getIntent().getStringExtra("order_type");
        }
        setBackOnclickListner(mContext);
        viewPager = $(R.id.viewpager_course_certain_list);
        tv_approveed = $(R.id.tv_course_certain_approveed);
        tv_unapprove = $(R.id.tv_course_certain_unapprove);
        tv_drafts = $(R.id.tv_course_certain_drafts);
        tv_revocation = $(R.id.tv_course_certain_revocation);
        line_approveed = $(R.id.line_course_certain_approveed);
        line_unapprove = $(R.id.line_course_certain_unapprove);
        line_drafts = $(R.id.line_course_certain_drafts);
        line_revocation = $(R.id.line_course_certain_revocation);
        setImgRightResid(R.mipmap.search_white_icon);
    }

    @Override
    protected void initdatas() {
        super.initdatas();
        fragments = new ArrayList<Fragment>();
        // 0 流转中 ,1 已完成 ,2 待撤销 ,3 已冻结
        fragments.add(CourseCertainListFragment.newInstance("1", order_type));
        fragments.add(CourseCertainListFragment.newInstance("0", order_type));
        fragments.add(CourseCertainListFragment.newInstance("", order_type));
        fragments.add(CourseCertainListFragment.newInstance("2", order_type));
        adapter = new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(fragments.size());
        setViewStatus(0);

    }

    @Override
    protected void initEvents() {
        super.initEvents();
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setViewStatus(0);
                        break;
                    case 1:
                        setViewStatus(1);
                        break;
                    case 2:
                        setViewStatus(2);
                        break;
                    case 3:
                        setViewStatus(3);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        tv_approveed.setOnClickListener(onClickListener);
        tv_unapprove.setOnClickListener(onClickListener);
        tv_drafts.setOnClickListener(onClickListener);
        tv_revocation.setOnClickListener(onClickListener);
        setImgReghtClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SearchInputCourseCertainMessageUIActivity.class);
                intent.putExtra("order_type", order_type);
                intent.putExtra("order_status", order_status);
                startActivity(intent);
            }
        });
    }

    /**
     * 控件的点击事件
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_course_certain_approveed:
                    setViewStatus(0);
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.tv_course_certain_unapprove:
                    setViewStatus(1);
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.tv_course_certain_drafts:
                    setViewStatus(2);
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.tv_course_certain_revocation:
                    setViewStatus(3);
                    viewPager.setCurrentItem(3);
                    break;
            }
        }
    };

    /**
     * 修改view的状态
     *
     * @param position
     */
    private void setViewStatus(int position) {
        order_status = position;
        tv_approveed.setTextColor(getResources().getColor(R.color.gray_666));
        tv_unapprove.setTextColor(getResources().getColor(R.color.gray_666));
        tv_drafts.setTextColor(getResources().getColor(R.color.gray_666));
        tv_revocation.setTextColor(getResources().getColor(R.color.gray_666));
        line_approveed.setVisibility(View.INVISIBLE);
        line_unapprove.setVisibility(View.INVISIBLE);
        line_drafts.setVisibility(View.INVISIBLE);
        line_revocation.setVisibility(View.INVISIBLE);
        switch (position) {
            case 0:
                tv_approveed.setTextColor(getResources().getColor(R.color.home_color));
                line_approveed.setVisibility(View.VISIBLE);
                break;
            case 1:
                tv_unapprove.setTextColor(getResources().getColor(R.color.home_color));
                line_unapprove.setVisibility(View.VISIBLE);
                break;
            case 2:
                tv_drafts.setTextColor(getResources().getColor(R.color.home_color));
                line_drafts.setVisibility(View.VISIBLE);
                break;
            case 3:
                tv_revocation.setTextColor(getResources().getColor(R.color.home_color));
                line_revocation.setVisibility(View.VISIBLE);
                break;
        }
    }
}
